package view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import java.util.ArrayList;

import mainFragment.fragment.ZhaoShengSituation;

/**
 * Created by Administrator on 2017/3/6.
 */

public class ZhaoShengSituationSelector extends Dialog {
    private View view;
    private Context context;
    private ListView listView;
    private ArrayList<String> arrayList = new ArrayList<>();
    private MyAdapter adapter;
    private String defaultProvince;
    private Handler handler;
    private int flag;


    public ZhaoShengSituationSelector(Context context) {
        super(context);
        this.context = context;
    }

    public ZhaoShengSituationSelector(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public ZhaoShengSituationSelector(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    public void setList(ArrayList<String> arrayList, String defaultProvince, Handler handler, int flag) {
        this.arrayList = arrayList;
        this.defaultProvince = defaultProvince;
        this.handler = handler;
        this.flag = flag;
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();

    }

    private void init() {
        view = LayoutInflater.from(context).inflate(R.layout.zhaoshengsituation_selector, null);
        listView = (ListView) view.findViewById(R.id.province_selector);
        setContentView(view);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = 550;
        window.setAttributes(lp);
        window.setAttributes(lp);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Message ms = new Message();
                switch (flag) {
                    case 0://选择省份
                        ms.what = ZhaoShengSituation.CHOSE_PROV;
                        break;
                    case 1://选择专业
                        ms.what = ZhaoShengSituation.CHOSE_MAJOR;
                        break;
                    default:
                        break;
                }
                ms.obj = arrayList.get(position);
                handler.sendMessage(ms);
            }
        });
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHolder myHolder;
            if (convertView == null) {
                myHolder = new MyHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.zhaoshengsituation_selector_item, null);
                myHolder.province = (TextView) convertView.findViewById(R.id.province_selector_item_tv);
                myHolder.checked = (ImageView) convertView.findViewById(R.id.province_selector_item_ima);
                if (defaultProvince.equals(arrayList.get(position))) {
                    myHolder.checked.setImageResource(R.mipmap.province_checked);
                } else {
                    myHolder.checked.setImageBitmap(null);
                }
                convertView.setTag(myHolder);
                myHolder.province.setText(arrayList.get(position));
            } else {
                myHolder = (MyHolder) convertView.getTag();
                myHolder.province.setText(arrayList.get(position));
                if (defaultProvince.equals(arrayList.get(position))) {
                    myHolder.checked.setImageResource(R.mipmap.province_checked);
                } else {
                    myHolder.checked.setImageBitmap(null);
                }

            }
            return convertView;
        }

        class MyHolder {
            TextView province;
            ImageView checked;
        }
    }


}
